package com.dckj.auto.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import com.dckj.auto.common.utils.MenuTreeUtil;
import com.dckj.auto.common.utils.userauto.MenuTree;
import com.dckj.auto.entity.AuthMenu;
import com.dckj.auto.entity.AuthMenuRole;
import com.dckj.auto.entity.AuthUserRole;
import com.dckj.auto.dao.AuthUserRoleDao;
import com.dckj.auto.service.AuthMenuRoleService;
import com.dckj.auto.service.AuthMenuService;
import com.dckj.auto.service.AuthUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangDong123
 * @since 2018-09-03
 */
@Service
public class AuthUserRoleServiceImpl extends ServiceImpl<AuthUserRoleDao, AuthUserRole> implements AuthUserRoleService {

    @Autowired
    AuthUserRoleService userRoleService;
    @Autowired
    AuthMenuRoleService menuRoleService;
    @Autowired
    AuthMenuService menuService;


    @Override
    public JSONArray selectUserAuto(String userId, Integer userType, Integer attribute) {
        List<AuthMenu> list = new ArrayList<>();
        if(userType.equals(0)){//超级挂管理员有所有权限
            list = menuService.list(attribute);
        }else{
            List<Map<String, Object>> maps = userRoleService.selectMaps(new EntityWrapper<AuthUserRole>()
                    .setSqlSelect("role_id").eq("user_id", userId));
            List<String> roleIs = new ArrayList<>();
            for (Map map : maps){
                String roleId = (String) map.get("role_id");
                roleIs.add(roleId);
            }
            List<Map<String, Object>> menuMap = new ArrayList<>();
            if(roleIs.size()>0){
                menuMap = menuRoleService.selectMaps(new EntityWrapper<AuthMenuRole>()
                        .setSqlSelect("menu_id").in("role_id", roleIs).eq("menu_attribute",attribute));
            }
            List<String> menuIds = new ArrayList<>();
            for (Map map : menuMap){
                String menuId = (String) map.get("menu_id");
                menuIds.add(menuId);
            }
            if(menuIds.size()>0){
                list = menuService.selectList(new EntityWrapper<AuthMenu>().in("menu_id",menuIds).orderBy("num+authority_id"));
            }
        }
        if(list.size()<=0){
            return new JSONArray();
        }
        List<MenuTree> menuTrees = new ArrayList<>();
        for (AuthMenu menu : list){

            MenuTree menuTree = new MenuTree();
            menuTree.setId(menu.getAuthorityId()+"");
            menuTree.setPid(menu.getParentId()+"");
            menuTree.setTitle(menu.getName());
            menuTree.setIcon(menu.getIcon());
            menuTree.setSpread(Boolean.valueOf(menu.getSpread()));
            menuTree.setUrl(menu.getUrl());
            menuTree.setCode(menu.getCode());
            menuTrees.add(menuTree);
        }
        MenuTree rootNode = MenuTreeUtil.getRootNode(menuTrees, menuTrees.get(0));
        String tree = "["+ JSONUtil.toJsonStr(rootNode)+"]";
        return StringUtils.isNotEmpty(tree)?JSONArray.parseArray(tree):new JSONArray();
    }



}
